<style lang="less">
	@import '../../assets/style/index.less';
</style>

<template>
	<div class="index">
		<!-- 页面上部分 开始 -->
		<div class="top_cont">
			<van-row>
				<van-col span="24">
					<span class="tit">{{title}}</span>
				</van-col>
				<!-- 登录人信息 开始 -->
				<van-col span="22" offset="1" class="item">
					<img :src="userImage" alt="" class="adm">
					<div class="con_r">
						<span class="user">{{userName}}</span>
						<van-button v-if='jobName' class="btn" size="mini" :square="show" disabled>
							{{jobName}}
						</van-button>
						<van-button v-if='workType' class="btn" size="mini" :square="show" disabled>
							{{workType}}
						</van-button>
						<van-button v-if='roleName' class="btn" size="mini" :square="show" disabled>
							{{roleName}}
						</van-button>
					</div>
				</van-col>
				<!-- 登录人信息 结束 -->
			</van-row>
			<!-- 得分与排名 开始 -->
			<van-row class="weather">
				<van-col span="6">
					<span class="num">{{weaVal01}}</span>
					<span class="text">今日天气</span>
				</van-col>
				<van-col span="6">
					<!--weaVal01-->
					<span class="num">{{weaVal02}}</span>
					<span class="text">温度</span>
				</van-col>
				<van-col span="6">
					<span class="num">{{weaVal03}}</span>
					<span class="text">空气质量</span>
				</van-col>
				<van-col span="6">
					<span class="num">{{weaVal04}}</span>
					<span class="text">风力</span>
				</van-col>
			</van-row>
			<!-- 得分与排名 结束 -->
		</div>
		<!-- 页面上部分 结束 -->

		<Grid></Grid>
		<!-- 本月任务 -->
		<div class="ywc_task">
			<van-row>
				<van-col span="6">
					<span class="tit">已完成/总数</span>
					<span class="titrw">本月任务</span>
				</van-col>
				<van-col span="6">
					<span class="txt">{{finish.cgTaskNum}}/{{total.cgTaskNum}}</span>
					<span class="tit">常规巡视</span>
				</van-col>
				<van-col span="6">
					<span class="txt">{{finish.tsTaskNum}}/{{total.tsTaskNum}}</span>
					<span class="tit">特殊巡视</span>
				</van-col>
				<van-col span="6">
					<span class="txt">{{finish.bdTaskNum}}/{{total.bdTaskNum}}</span>
					<span class="tit">保电值守</span>
				</van-col>
			</van-row>
		</div>
	</div>
</template>

<script>
	import {
		Row,
		Col,
		Button,
		Toast
	} from 'vant';

	import touXiang from '../../assets/img/index/touxiang.png';
	import Grid from '../components/grid'; //引入首页九宫格
	import { callData, fileUrl } from '../../axios/axios'
	import { setSession, getSession } from '../../libs/cache'
	import { startMpush, getLocation } from '../../api/android'
	export default {
		name: 'IndexHome',
		data() {
			return {
				title: '智能巡检', //标题
				userName: 'admin', //用户名
				jobName: "",
				workType: "",
				roleName: "",
				cname: "grid", //类名
				show: true, //接单人
				userImage: touXiang, //头像图标
				finish:{//已完成任务
					cgTaskNum: 0, //常规巡视数
					tsTaskNum: 0, //特殊巡视数
					jxTaskNum:0,//线路检修
					bdTaskNum: 0 //保电值守
				},
				total:{//未完成任务
					cgTaskNum: 0, //常规巡视数
					tsTaskNum: 0, //特殊巡视数
					jxTaskNum:0,//线路检修
					bdTaskNum: 0 //保电值守
				},
				lon:'116.313968',		 //安卓径度  
				lat:'40.053048',			//安卓纬度
				weaVal01:'',
				weaVal02:'',
				weaVal03:'',
				weaVal04:''
			}
		},
		components: {
			Grid,
			[Row.name]: Row,
			[Col.name]: Col,
			[Button.name]: Button
		},
		methods: {
			// 获取登录人信息
			getUserMeg() {
				if(!getSession('userMeg')) {
					callData('/userCenter/CmUser/currentUserInfo', 'get', {}).then(data => {
						if(data.success) {
							// 登录时获取登录人姓名
							let user = data.data;
							this.userName = user.realname;
							this.jobName = user.jobName;
							this.workType = user.workType;
							this.roleName = user.roleName;
							if(user.avatar) this.userImage = fileUrl(user.avatar)
							else this.userImage = touXiang
							// 将登录人信息存在sessionStorage中
							setSession('userMeg', JSON.stringify(data.data))
							if(window.Cordova){
								startMpush(user.id)
							}
						}
					})
				} else {
					let user = JSON.parse(getSession("userMeg"))
					this.userName = user.realname
					this.jobName = user.jobName;
					this.workType = user.workType;
					this.roleName = user.roleName;
					if(user.avatar) this.userImage = fileUrl(user.avatar)
					else this.userImage = touXiang
				}
			},
			//获取已完成未完成任务
			initData(){
				const _this = this;
				callData('/statisServer/statis/appTaskZhu', 'get', {}).then(data => {
					data.data.forEach(v => {
						if (v.TASKTYPE === '0') {
							_this.finish['cgTaskNum'] = v.YWC;
							_this.total['cgTaskNum'] = v.TOTAL;
						} else if (v.TASKTYPE === '1') {
							_this.finish['tsTaskNum'] = v.YWC;
							_this.total['tsTaskNum'] = v.TOTAL;
						}else if(v.TASKTYPE === '2'){
							_this.finish['bdTaskNum'] = v.YWC;
							_this.total['bdTaskNum'] = v.TOTAL;
						}
					})
				})
			},
			//获取当前的位置天气
			getWeather(){
				let weather = getSession('weather');
				if(!weather) {
					this.getAddress()
				}else{
					weather = JSON.parse(weather);
					this.weaVal01 = weather.weaVal01;//天气
					this.weaVal02 = weather.weaVal02;//温度
					this.weaVal03 = weather.weaVal03;//空气质量
					this.weaVal04 = weather.weaVal04;//风力
				}
			},
			// 获取当前的位置信息
			getAddress(){
				if(window.Cordova){
					getLocation(res => {
						if(res.success){
							this.jdwdMeg = {
								lon:res.data.lon, // 经度
								lat:res.data.lat // 纬度
							}
							this.lon = res.data.lon;// 经度
							this.lat = res.data.lat;// 纬度
							this.appWeather()
						}
					})
				}else{
					this.appWeather()
				}
			},
			//调取天气接口
			appWeather(){
				let params = {};
				let _this = this;
				params.jd = this.lon;
				params.wd = this.lat;
				callData('/statisServer/Weather/appWeather', 'get', params).then(data => {
					if(data.success){
						let resData = data.data;
						let weather1 = {};
						_this.weaVal01=resData.weather;//天气
						_this.weaVal02=resData.temperature;//温度
						_this.weaVal03=resData.quality;//空气质量
						_this.weaVal04=resData.wind_power;//风力
						weather1.weaVal01 = _this.weaVal01;
						weather1.weaVal02 = _this.weaVal02;
						weather1.weaVal03 = _this.weaVal03;
						weather1.weaVal04 = _this.weaVal04;
						setSession('weather', JSON.stringify(weather1))
					}else{
						Toast({
							message: data.data,
							position: "bottom"
						});
					}
				})
			}
		},
		mounted() {
			this.getUserMeg()
			this.initData()
			this.getWeather()
		}
	}
</script>